<!--
 * @Author: JiraiyaChen 979526234@qq.com
 * @Date: 2025-03-10 14:01:18
 * @LastEditors: JiraiyaChen 979526234@qq.com
 * @LastEditTime: 2025-06-20 10:29:29
 * @Description: 项目评分标准配置 (无分页)
-->

<template>
  <div>
    <div class="standard-title flex items-center gap-10">
      <div class="title">项目评分标准配置</div>
      <div class="icon" @click="onLock">
        <SvgIcon :icon-class="lock ? 'lock' : 'unlock'" />
        <span class="text-sm">{{ lock ? '已锁定' : '已解锁' }}</span>
      </div>
    </div>
    <div class="flex justify-end">
      <el-button type="primary" plain @click="handleAddTemplate">选择模板</el-button>
      <el-button type="primary" plain :disabled="!multipleSelection.length" @click="handleDelete(multipleSelection)">
        删除
      </el-button>
      <el-button type="primary" @click="onAdd">新增</el-button>
    </div>
    <el-table
      :data="tableData"
      class="cl-table"
      table-layout="auto"
      tooltip-effect="light"
      style="width: 100%"
      header-cell-class-name="my-header-cell-row"
      cell-class-name="my-cell-row"
      :summary-method="getSummaries"
      :show-summary="isShowSummary"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="评审项名称" prop="name" />
      <el-table-column label="流程顺序" prop="flowOrderName" width="100" />
      <el-table-column label="打分方式" prop="markModeName" width="120" />
      <el-table-column label="汇总方式" prop="scoreCollectWayName" width="200" />
      <el-table-column label="最高分" prop="maxSummaryScore" width="100" />
      <el-table-column label="是否纳入总分" prop="includeName" width="120">
        <template #default="scope">
          <RowStage :row-data="scope.row" :column="{ prop: 'includeName' }" />
        </template>
      </el-table-column>
      <el-table-column label="是否暗标" prop="darkMarkName" width="100">
        <template #default="scope">
          <RowStage :row-data="scope.row" :column="{ prop: 'darkMarkName' }" />
        </template>
      </el-table-column>
      <el-table-column v-if="!lock" label="操作" width="250">
        <template #default="scope">
          <el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
          <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button link type="primary" @click="onAddScoringPoints(scope.row)">评分点</el-button>
        </template>
      </el-table-column>
      <el-table-column v-else label="操作" width="100">
        <template #default="scope">
          <el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 导入模板 -->
    <AddModel
      v-if="hookVisible"
      v-model="hookVisible"
      :info="dialogInfo"
      :type="type"
      @at-close="(type) => onCloseAddModel(type)"
    />
    <!-- 新增评审项 -->
    <AddCommonModel
      v-if="hookVisible2"
      v-model="hookVisible2"
      :info="dialogInfo2"
      :type="type2"
      :save-api="updateProjectBandInfo"
      @at-close="onCloseDialog2"
    />
    <!-- 新增评分点 -->
    <RatingPointsDialog
      v-if="hookVisible3"
      v-model="hookVisible3"
      :info="dialogInfo3"
      :type="type3"
      @at-close="(type) => onCloseRatingPoints(type)"
    />
  </div>
</template>

<script setup lang="ts">
import { ElMessageBox } from 'element-plus'
import AddModel from './AddModel.vue'
import RowStage from '@/components/row-stage/index.vue'
import AddCommonModel from '@/components/AddCommonModel/index.vue'
import {
  projectBandInfo,
  projectBandInfoDelete,
  projectPointInfoSave,
  updateLockStatus
} from '@/api/project-scoring-standard-configuration'
import RatingPointsDialog from '@/components/AddCommonModel/RatingPointsDialog.vue'
onMounted(() => {
  getTableData()
})
const totalScore = ref<number>(0)
const tableData = ref<any[]>([])
const isShowSummary = ref(false)
const getTableData = async () => {
  try {
    const { lockStatus, itemList, total }: any = await projectBandInfo()
    lock.value = lockStatus === 1
    totalScore.value = total
    tableData.value = itemList
    isShowSummary.value = tableData.value.length > 0
  } catch (error) {
    console.log(error)
  }
}

const getSummaries = (param) => {
  const { columns, data } = param
  const sums: any[] = []
  columns.forEach((column, index) => {
    if (index === 1) {
      sums[index] = '汇总情况'
      return
    }
    if (column.property === 'maxSummaryScore') {
      sums[index] = totalScore.value
      return
    }
  })
  return sums
}
// 多选
const multipleSelection = ref([])
const handleSelectionChange = (selection) => {
  multipleSelection.value = selection
}

// 选择模板
const { type, hookVisible, dialogInfo, onOpenDialog } = useDialog(null)
const handleAddTemplate = () => {
  type.value = '选择模板'
  onOpenDialog({})
}
const onCloseAddModel = (type: string | undefined) => {
  if (type === 'refresh-table') {
    getTableData()
  }
  hookVisible.value = false
}

const {
  type: type2,
  hookVisible: hookVisible2,
  dialogInfo: dialogInfo2,
  onOpenDialog: onOpenDialog2,
  onCloseDialog: onCloseDialog2
} = useDialog(null)
// 新增 评审项
const onAdd = async () => {
  type2.value = '新增项目评审项'
  onOpenDialog2({})
}
// 编辑 评审项
const handleEdit = (row: any) => {
  type2.value = '编辑项目评审项'
  onOpenDialog2(row)
}
// 查看 评审项
const handleView = (row: any) => {
  type2.value = '查看项目评审项'
  onOpenDialog2({ disabled: true, ...row })
}

// 新增 评分点
const { type: type3, hookVisible: hookVisible3, dialogInfo: dialogInfo3, onOpenDialog: onOpenDialog3 } = useDialog(null)
const onAddScoringPoints = async (row: any) => {
  type3.value = '项目评分点'
  onOpenDialog3(row)
}
const onCloseRatingPoints = async (type: string | undefined) => {
  if (type === 'refresh-table') {
    await getTableData()
  }
  hookVisible3.value = false
}

// 保存
const updateProjectBandInfo = async (params: any) => {
  try {
    await projectPointInfoSave(params)
    await getTableData()
  } catch (error) {
    console.log(error)
  }
}

const handleDelete = (multipleSelection: any) => {
  ElMessageBox.confirm('确定删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const ids = multipleSelection.map((item: any) => item.id)
      await projectBandInfoDelete(ids)
      await getTableData()
    } catch (error) {
      console.log(error)
    }
  })
}

// 锁定 解锁
const lock = ref(false)
const onLock = async () => {
  try {
    const lockStatus = lock.value ? 0 : 1
    await updateLockStatus({ lockStatus })
    await getTableData()
  } catch (error) {
    console.log(error)
  }
}
</script>
<style scoped>
@import url('@/components/cl-table/index.scss');
</style>
<style scoped lang="scss">
.standard-title {
  margin-bottom: 40px;
  font-size: 30px;

  .title {
    font-weight: 700;
    color: #2a2b34;
  }

  .icon {
    cursor: pointer;
    width: 40px;
    height: 40px;
    font-size: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    .text-sm {
      position: absolute;
      top: 10px;
      right: -50px;
      font-size: 12px;
      padding: 0 5px;
      color: #b1b3b8;
    }
  }
}
</style>
